﻿using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.Repository
{
    /// <summary>
    /// ④仓储层实现仓储接口，下面是配置webapi的webconfig的autofac映射关系
    /// </summary>
    public class SortDetailRepository:BaseRepository<SortDetail>,ISortDetailRepository
    {
        /// <summary>
        /// 分页获取类别下的类别明细
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="sortIds"></param>
        /// <returns></returns>
        public IEnumerable<SortDetail> LoadInSorts(int pageindex, int pagesize, params Guid[] sortIds)
        {
            return LoadInSorts(sortIds).OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            Delete(u => u.Id == id);
        }

        /// <summary>
        /// 获取类别下的类别明细
        /// </summary>
        /// <param name="sortId"></param>
        /// <returns></returns>
        public IEnumerable<SortDetail> LoadInSorts(params Guid[] sortId)
        {
            bool isZero = sortId.Length == 1 && sortId[0] == Guid.Empty;  //判断传进来的是否为0
            var result = from SortDetail in Context.SortDetails.Where(u => isZero
                         || u.CascadeId != null && sortId.Contains(u.SortId.Value))
                         select SortDetail;

            return result;
        }
        /// <summary>
        /// 分页获取类别明细
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IEnumerable<SortDetail> LoadSortDetails(int pageindex, int pagesize)
        {
            return Context.SortDetails.OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }
        /// <summary>
        /// 获取类别下类别明细数据条数
        /// </summary>
        /// <param name="sortIds"></param>
        /// <returns></returns>
        public int GetSortDetailCntInSorts(params Guid[] sortIds)
        {
            return LoadInSorts(sortIds).Count();
        }
    }
}
